package com.goddess.yeecolor.activity.myFragmentItemActivityAll;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v7.widget.Toolbar;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.goddess.yeecolor.R;
import com.goddess.yeecolor.activity.MainActivity;
import com.goddess.yeecolor.base.BaseActivity;
import com.goddess.yeecolor.tools.JumpIntents;
import com.goddess.yeecolor.tools.MyUrl;
import com.goddess.yeecolor.tools.OkttpUtils;
import com.goddess.yeecolor.tools.SharePreceUtils;
import com.goddess.yeecolor.tools.ToastUtils;
import com.goddess.yeecolor.view.dialog_progress;
import com.goddess.yeecolor.view.sharePopuWindow;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * 加入优雅荟
 */
public class JoinYouyasActivity extends BaseActivity {
    @InjectView(R.id.title_back)
    ImageView titleBack;
    @InjectView(R.id.register_enroll)
    Button registerEnroll;
    private TextView mTitle;
    private Toolbar mToobar;
    private View mView;
    private WebView webView;
    private TextView mPrices;
    private Button btnStart;
    private LinearLayout shang;
    private View view;
    private TextView nameText, numberText;
    private sharePopuWindow sharePopuWindow;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        setTheme(R.style.AppTheme);
        super.onCreate(savedInstanceState);
        mView = LayoutInflater.from(this).inflate(R.layout.activity_jonin, null);
        setContentView(mView);
        ButterKnife.inject(this);
        initFindView();
        initOnClck();
    }
    private dialog_progress dialog_progress;
    private void setProgress(){
        dialog_progress=new dialog_progress(JoinYouyasActivity.this);
        dialog_progress.requestWindowFeature(Window.FEATURE_NO_TITLE);

        dialog_progress.setCanceledOnTouchOutside(false);//按返回键可以关闭进度条
        dialog_progress.show();
    }

    private String price;
    @Override
    public void initOnClck() {
        btnStart.setOnClickListener((view) -> {
            setProgress();
            price = mPrices.getText().toString().trim();
            Map<Object, Object> map = new HashMap<>();
            map.put("unique", SharePreceUtils.getOther("user", "unique"));
            OkttpUtils.getIntence().OkttpPost(MyUrl.JOINHUIYUAN, map, handler, 2);

        });
    }

    @Override
    protected void onResume() {
        super.onResume();

        initData();
    }

    @Override
    public void initFindView() {

        nameText = (TextView) findViewById(R.id.name);
        numberText = (TextView) findViewById(R.id.number);
        webView = (WebView) findViewById(R.id.webview);
        shang = (LinearLayout) findViewById(R.id.shang);
        view = findViewById(R.id.xia);
        mToobar = (Toolbar) findViewById(R.id.title_toobar);
        mTitle = (TextView) findViewById(R.id.title_content);
        mTitle.setText("加入优雅荟");
        mToobar.setNavigationIcon(R.mipmap.back3x);
        mToobar.setNavigationOnClickListener((view) -> {
            setFiish(this);
        });

        mPrices = (TextView) findViewById(R.id.join_pices);
        btnStart = (Button) findViewById(R.id.join_enroll);
    }

    @Override
    public void initData() {
        String svipStateLogin = SharePreceUtils.getOther("svip", "svipState");
        String svipStateMy = getIntent().getStringExtra("state");
        if (svipStateLogin.equals("1")||svipStateMy.equals("1")) {
            shang.setVisibility(View.GONE);
            view.setVisibility(View.VISIBLE);
        }
        String fileUrl = "file:///android_asset/openvip.html";
        webView.getSettings().setDefaultTextEncodingName("utf-8");
        webView.loadUrl(fileUrl);
        webView.setWebViewClient(new WebViewClient());
        Map<Object, Object> map = new HashMap<>();
        map.put("unique", SharePreceUtils.getOther("user", "unique"));
        OkttpUtils.getIntence().OkttpPost(MyUrl.HUIYUAN, map, handler, 1);
    }


    private String jsonString;
    private String state = "0";
    private String name = "";
    private String number = "";
    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            try {
                jsonString = msg.getData().getString("result");
                JSONObject object = new JSONObject(jsonString);
                int code = object.getInt("code");
                if (code == 100) {
                    JSONObject result = object.getJSONObject("result");
                    if (msg.what == 1) {
                        price = result.optString("price");
                        mPrices.setText(price);
                        number = result.optString("number");
                        JSONObject jsonObject = result.getJSONObject("svip");
                        state=jsonObject.optString("state");
                        if (state.equals("1")){
                            JSONObject jsonObject1 = jsonObject.optJSONObject("basic");
                            name = jsonObject1.optString("nickname");
                        }

                    } else if (msg.what == 2) {
                        String order_id = result.optString("order_id");
                        ToastUtils.setToastText(object.optString("extends"), JoinYouyasActivity.this);
                        SharePreceUtils.saveOrder(order_id, order_id, price);
                        if (dialog_progress!=null&&dialog_progress.isShowing()){
                            dialog_progress.dismiss();
                        }
                        JumpIntents.startIntentCan(JoinYouyasActivity.this, PaymentActivity.class, order_id);
                    }
                } else if (code == 403) {
                    String mssg = object.getString("msg");
                }
                nameText.setText(name);
                numberText.setText("No." + number);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    };

    @Override
    public void onBackPressed() {
        super.onBackPressed();
        finish();
        overridePendingTransition(0, 0);
    }
    @OnClick({R.id.title_back, R.id.register_enroll})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.title_back://返回按钮
                setFiish(this);
                break;
            case R.id.register_enroll://浏览课程按钮
                SharePreceUtils.saveOne("type",2,"show");//返回课程
                JumpIntents.startIntent(this, MainActivity.class);
                break;
        }
    }
}
